//
//  PersonalInfomationViewController.m
//  PigMarket
//
//  Created by mac on 2019/6/13.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "PersonalInfomationViewController.h"
#import "MeHeaderView.h"
#import "PhotosView.h"
#import "ChatViewController.h"
@interface PersonalInfomationViewController ()

@property (strong, nonatomic) UIButton *leftNavBtn;

/**
 headerViewbg
 */
@property (nonatomic , strong) UIImageView *headerViewbg;
/**
 头部个人信息
 */
@property (nonatomic , strong) MeHeaderView *headerView;
/**
 photoBgView
 */
@property (nonatomic , strong) UIView *photoBgView;
/**
 他的照片lab
 */
@property (nonatomic , strong) UILabel *hisPhotoLab;
/**
 横线
 */
@property (nonatomic , strong) UIView *line;
/**
 照片View
 */
@property (nonatomic , strong) PhotosView *photoView;

/**
 加好友和进入店铺的bgView
 */
@property (nonatomic , strong) UIView *bottomView;
/**
 加好友
 */
@property (nonatomic , strong) UIButton *addFriendBtn;
/**
 进店铺
 */
@property (nonatomic , strong) UIButton *gotoShopBtn;
/**
 竖线
 */
@property (nonatomic , strong) UIView *sLine;
/**
 店铺数量icon
 */
@property (nonatomic , strong) UIImageView *shopNumIcon;

/**
 chatUserInfoModel
 */
@property (nonatomic , strong) ChatUserInfo *chatUserInfoModel;

@end

@implementation PersonalInfomationViewController


#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.baseNavView.hidden = YES;
    [self setUpView];
}


#pragma mark - init method

- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}


- (void)initView {
    [self.view addSubview:self.headerViewbg];
    [self.headerViewbg addSubview:self.headerView];
    [self.headerViewbg addSubview:self.leftNavBtn];

    
    [self.view addSubview:self.photoBgView];
    [self.photoBgView addSubview:self.hisPhotoLab];
    [self.photoBgView addSubview:self.line];
    [self.photoBgView addSubview:self.photoView];
    
    [self.view addSubview:self.bottomView];
    [self.bottomView addSubview:self.sLine];
    [self.bottomView addSubview:self.addFriendBtn];
    [self.bottomView addSubview:self.gotoShopBtn];
    [self.bottomView addSubview:self.shopNumIcon];

}


- (void) initLayout {
    
    [self.headerViewbg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(TJ_StatusBarHeight_Define + 164);
    }];
    
    {
        _leftNavBtn.frame = CGRectMake([self fitSize:15], TJ_StatusBarHeight_Define, 44,  TJ_NavBarHeight-TJ_StatusBarHeight_Define);
    }
    
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(self.headerViewbg.mas_height);
    }];
    
    [self.photoBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(self.headerViewbg.mas_bottom).offset(0);
        make.height.mas_equalTo(80+(SCREEN_WIDTH - 60 ) *2 / 3);
    }];
    [self.hisPhotoLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.top.mas_equalTo(20);
        make.height.mas_equalTo(20);
    }];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(1);
        make.top.mas_equalTo(self.hisPhotoLab.mas_bottom).offset(12);
    }];
    [self.photoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.right.mas_equalTo(-12);
        make.top.mas_equalTo(self.line.mas_bottom).offset(12);
        make.height.mas_equalTo((SCREEN_WIDTH - 60 ) *2 / 3);
    }];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(58);
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(-IPHONEXBottomEdge);
    }];
    [self.sLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.bottomView.mas_centerX);
        make.centerY.mas_equalTo(self.bottomView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(1, 18));
    }];
    [self.addFriendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.bottomView.mas_centerX).multipliedBy(0.5);
        make.height.mas_equalTo(58);
    }];
    [self.gotoShopBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.bottomView.mas_centerX).multipliedBy(1.5);
        make.height.mas_equalTo(58);
    }];
    [self.shopNumIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.gotoShopBtn.mas_right).offset(1);
        make.centerY.mas_equalTo(self.gotoShopBtn.mas_centerY);
    }];

}

- (void) initAction {
    
    [self.addFriendBtn addTarget:self action:@selector(addFriendBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.gotoShopBtn addTarget:self action:@selector(gotoShopBtnClick) forControlEvents:UIControlEventTouchUpInside];
    //照片点击
    @weakify(self);
    [self.photoView setPhotoTappBlock:^(NSInteger index) {
        @strongify(self);
        [self checkBigPhotoWithIndex:index];
    }];
}

- (void)initData {
    NSString *myMemberNum = [NSString stringWithFormat:@"%@",[UserInfoDataProvider sharedInstance].userInfo.memberNum];
    @weakify(self);
    [[[PigMarketApi sharedInstance] getChatUserInfoWithMemberNum:self.memberNum myMemberNum:myMemberNum] subscribeNext:^(ChatUserInfo * _Nullable chatUserInfo) {
        @strongify(self);
        self.headerView.chatUserInfoModel = chatUserInfo;
        [self.photoView configPhotosWithImgsArr:chatUserInfo.imgs];
        self.chatUserInfoModel = chatUserInfo;
        if ([chatUserInfo.shopNum intValue] > 1) {
            self.shopNumIcon.hidden = NO;
        } else {
            self.shopNumIcon.hidden = YES;
        }
        switch ([chatUserInfo.friendIf intValue]) {
            case IsFrinedStatusYes:
            {
                [self.addFriendBtn setTitle:@" 聊一聊" forState:UIControlStateNormal];
                self.addFriendBtn.tag = 100;
            }
                break;
            case IsFrinedStatusNo:
            {
                [self.addFriendBtn setTitle:@" 加好友" forState:UIControlStateNormal];
                self.addFriendBtn.tag = 101;
            }
                break;
            default:
                break;
        }
        
    } error:^(NSError * _Nullable error) {
        
        @strongify(self);
        [self handlingError:error];
    }];
}

#pragma mark - delegate

#pragma mark - target action


//加好友 聊一聊
- (void)addFriendBtnClick:(UIButton *)button {
    if (button.tag == 100) {
        TUIConversationCellData *data = [[TUIConversationCellData alloc] init];
        data.convId = self.memberNum;
        data.convType = TIM_C2C;
        data.title = self.chatUserInfoModel.nickName;
        ChatViewController *chat = [[ChatViewController alloc] init];
        chat.conversation = data;
        [self.navigationController pushViewController:chat animated:YES];

    } else if (button.tag == 101){
        //添加好友
        AddFriendVerifyViewController *vc = [[AddFriendVerifyViewController alloc] init];
        vc.identifier = self.chatUserInfoModel.memberNum;
        [self.navigationController pushViewController:vc animated:YES];

    }
}


//查看店铺
- (void)gotoShopBtnClick {
    
}


#pragma mark - private method

- (void)checkBigPhotoWithIndex:(NSInteger)index {
    PMLog(@"%ld",(long)index);
}


#pragma mark - getter and setter
- (UIImageView *)headerViewbg {
    if (!_headerViewbg) {
        _headerViewbg = [[UIImageView alloc] init];
        _headerViewbg.image = [UIImage imageNamed:@"user_headerviewbg"];
        _headerViewbg.userInteractionEnabled = YES;
    }
    return _headerViewbg;
}

- (UIButton *)leftNavBtn{
    
    if (!_leftNavBtn) {
        _leftNavBtn = [UIButton makeButtonWithImageName:@"nav_white"];
        _leftNavBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        
        kWeakSelf(self);
        _leftNavBtn.tapBlock = ^{
            [weakself.navigationController popViewControllerAnimated:YES];
        };
    }
    
    return _leftNavBtn;
}


- (MeHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[MeHeaderView alloc] init];
    }
    return _headerView;
}


- (UIView *)photoBgView {
    if (!_photoBgView) {
        _photoBgView = [[UIView alloc] init];
        _photoBgView.backgroundColor = [UIColor whiteColor];
    }
    return _photoBgView;
}


- (UILabel *)hisPhotoLab {
    if (!_hisPhotoLab) {
        _hisPhotoLab = [[UILabel alloc] init];
        _hisPhotoLab.textColor = [UIColor colorWithHexString:@"#262626"];
        _hisPhotoLab.font = [UIFont middleFont];
        _hisPhotoLab.text = @"Ta的照片";
    }
    return _hisPhotoLab;
}


- (UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = [UIColor colorWithHexString:@"#EAEAEA"];
    }
    return _line;
}


- (PhotosView *)photoView {
    if (!_photoView) {
        _photoView = [[PhotosView alloc] init];
    }
    return _photoView;
}


- (UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] init];
        _bottomView.backgroundColor = [UIColor whiteColor];
    }
    return _bottomView;
}


- (UIButton *)addFriendBtn {
    if (!_addFriendBtn) {
        _addFriendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addFriendBtn setTitleColor:[UIColor colorWithHexString:@"#666666"] forState:UIControlStateNormal];
        _addFriendBtn.titleLabel.font = [UIFont middleFont];
        [_addFriendBtn setTitle:@"    " forState:UIControlStateNormal];
        [_addFriendBtn setImage:[UIImage imageNamed:@"chat_nearby_addfriend"] forState:UIControlStateNormal];
    }
    return _addFriendBtn;
}


- (UIView *)sLine {
    if (!_sLine) {
        _sLine = [[UIView alloc] init];
        _sLine.backgroundColor = [UIColor colorWithHexString:@"#CBCBCB"];
    }
    return _sLine;
}


- (UIButton *)gotoShopBtn {
    if (!_gotoShopBtn) {
        _gotoShopBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_gotoShopBtn setTitle:@" 进店铺" forState:UIControlStateNormal];
        [_gotoShopBtn setTitleColor:[UIColor colorWithHexString:@"#666666"] forState:UIControlStateNormal];
        _gotoShopBtn.titleLabel.font = [UIFont middleFont];
        [_gotoShopBtn setImage:[UIImage imageNamed:@"meat_found_shopIcon"] forState:UIControlStateNormal];
    }
    return _gotoShopBtn;
}


- (UIImageView *)shopNumIcon {
    if (!_shopNumIcon) {
        _shopNumIcon = [[UIImageView alloc] init];
        _shopNumIcon.image = [UIImage imageNamed:@"meat_found_down"];
    }
    return _shopNumIcon;
}



@end
